package cn.icatw.yygh.hosp.controller.api;

import cn.icatw.yygh.common.exception.YyghException;
import cn.icatw.yygh.common.helper.HttpRequestHelper;
import cn.icatw.yygh.common.result.Result;
import cn.icatw.yygh.common.result.ResultCodeEnum;
import cn.icatw.yygh.common.utils.MD5;
import cn.icatw.yygh.hosp.service.DepartmentService;
import cn.icatw.yygh.hosp.service.HospitalService;
import cn.icatw.yygh.hosp.service.HospitalSetService;
import cn.icatw.yygh.hosp.service.ScheduleService;
import cn.icatw.yygh.model.hosp.Department;
import cn.icatw.yygh.model.hosp.Hospital;
import cn.icatw.yygh.model.hosp.Schedule;
import cn.icatw.yygh.vo.hosp.DepartmentQueryVo;
import cn.icatw.yygh.vo.hosp.ScheduleQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * ApiController
 *
 * @author 76218
 */
@RestController
@RequestMapping("/api/hosp")
public class ApiController {
    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;

    /**
     * 上传医院接口
     *
     * @param request 前端封装的hosp请求
     * @return 统一结果集
     */
    @PostMapping("saveHospital")
    public Result saveHosp(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, Object> paramMap = getParamMap(request);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //1.获取医院系统传递过来的签名,签名进行了MD5加密
        signatureVerification(paramMap, hoscode);
        //传输过程中“+”转换为了“ ”，因此我们要转换回来
        String logoData = (String) paramMap.get("logoData");
        logoData = logoData.replace(" ", "+");
        paramMap.put("logoData", logoData);
        //调用service中的方法
        hospitalService.save(paramMap);
        return Result.ok();
    }

    /**
     * 根据医院编号进行查询
     *
     * @param request 前端请求
     * @return 医院信息
     */
    @PostMapping("hospital/show")
    public Result getHospital(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, Object> paramMap = getParamMap(request);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //1.获取医院系统传递过来的签名,签名进行了MD5加密
        signatureVerification(paramMap, hoscode);
        //调用service方法实现根据医院编号查询
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    /**
     * 上传科室接口
     *
     * @param request 前端请求
     * @return 统一结果集
     */
    @PostMapping("saveDepartment")
    public Result saveDepartment(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, Object> paramMap = getParamMap(request);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //签名校验
        signatureVerification(paramMap, hoscode);
        //调用service的方法
        departmentService.save(paramMap);
        return Result.ok();
    }

    /**
     * 查询科室接口
     *
     * @param request 前端科室信息
     * @return 科室信息
     */
    @PostMapping("department/list")
    public Result findDepartment(HttpServletRequest request) {
        //获取传递过来的科室信息
        Map<String, Object> paramMap = getParamMap(request);
        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //当前页和每页记录数
        int page = StringUtils
                .isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils
                .isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        //1.获取医院系统传递过来的签名,签名进行了MD5加密
        signatureVerification(paramMap, hoscode);
        DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
        departmentQueryVo.setHoscode(hoscode);
        //调用service方法
        Page<Department> pageModel = departmentService.findPageDepartment(page, limit, departmentQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除科室接口
     *
     * @param request 前端请求
     * @return 统一结果集
     */
    @PostMapping("department/remove")
    public Result removeDepartment(HttpServletRequest request) {
        //获取传递过来的排班信息
        Map<String, Object> paramMap = getParamMap(request);
        //获取医院编号和科室编号
        String hoscode = (String) paramMap.get("hoscode");
        String depcode = (String) paramMap.get("depcode");
        //1.获取医院系统传递过来的签名,签名进行了MD5加密
        signatureVerification(paramMap, hoscode);
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }

    /**
     * 上传排班接口
     *
     * @param request 前端封装信息
     * @return 统一结果集
     */
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递过来的排班信息
        Map<String, Object> paramMap = getParamMap(request);
        //获取医院编号
        String hoscode = (String) paramMap.get("hoscode");
        signatureVerification(paramMap, hoscode);
        //调用service的方法
        scheduleService.save(paramMap);
        return Result.ok();
    }

    /**
     * 查询排班接口
     *
     * @param request 前端科室信息
     * @return 排班信息
     */
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        Map<String, Object> paramMap = getParamMap(request);
        //医院编号
        String hoscode = (String) paramMap.get("hoscode");
        //科室编号
        String depcode = (String) paramMap.get("depcode");
        //当前页和每页记录数
        int page = StringUtils
                .isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils
                .isEmpty(paramMap.get("limit")) ? 1 : Integer.parseInt((String) paramMap.get("limit"));
        //签名校验
        signatureVerification(paramMap, hoscode);
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hoscode);
        scheduleQueryVo.setDepcode(depcode);
        //调用service方法
        Page<Schedule> pageModel = scheduleService.findPageSchedule(page, limit, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 删除排班
     * @param request 前端信息
     * @return 统一结果集
     */
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        //获取传递过来的map集合
        Map<String, Object> paramMap = getParamMap(request);
        //获取医院编号和排班编号
        String hoscode = (String) paramMap.get("hoscode");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        //签名校验
        signatureVerification(paramMap, hoscode);
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }

    /**
     * 获取医院信息的paramMap集合
     *
     * @param request 前端信息
     * @return 对应的参数信息
     */
    private Map<String, Object> getParamMap(HttpServletRequest request) {
        //获取传递过来的信息
        Map<String, String[]> requestMap = request.getParameterMap();
        return HttpRequestHelper.switchMap(requestMap);
    }

    /**
     * 签名校验方法
     *
     * @param paramMap 前端封装信息
     * @param hoscode  医院编号
     */
    private void signatureVerification(Map<String, Object> paramMap, String hoscode) {
        //1.获取医院系统传递过来的签名,签名进行了MD5加密
        String hospSign = (String) paramMap.get("sign");
        //2.根据传递过来的医院编码，查询数据库，查询签名
        String signKey = hospitalSetService.getSignKey(hoscode);
        //3.把数据库签名进行MD5加密
        String signKeyMd5 = MD5.encrypt(signKey);
        //4.判断签名是否一致
        if (!hospSign.equals(signKeyMd5)) {
            throw new YyghException(ResultCodeEnum.SIGN_ERROR);
        }
    }
}
